using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WPS.Modules.StyleLibrary
{
	/// <summary>
	/// Show status and error messages on the screen, stored in session.
	/// </summary>
	public partial class PageMessageControl : System.Web.UI.UserControl
	{
		private readonly string MessageSessionKey = "tinymce:stylelibrary:message";
		private readonly string ErrorSessionKey = "tinymce:stylelibrary:error";

		#region Page events

		protected override void Render(System.Web.UI.HtmlTextWriter writer)
		{
			if(Session[this.MessageSessionKey] != null)
			{
				lblPageMessage.Text = (string)Session[this.MessageSessionKey];
				if((bool)Session[this.ErrorSessionKey])
				{
					lblPageMessage.Style["color"] = "red";
				}
				Clear();
				fldPageMessages.Visible = true;
			}
			else
			{
				fldPageMessages.Visible = false;
			}

			base.Render(writer);
		}

		#endregion

		/// <summary>
		/// Sends a message to te control which has been shown it in the Page_PreRender
		/// </summary>
		/// <param name="message">The message itself</param>
		/// <param name="error">True if the message is error, false if status message.</param>
		public void Send(string message, bool error)
		{
			Session[this.MessageSessionKey] = message;
			Session[this.ErrorSessionKey] = error;
		}

		/// <summary>
		/// Clears the current page message. Manually clearing is usually not necessary.
		/// </summary>
		public void Clear()
		{
			Session.Remove(this.ErrorSessionKey);
			Session.Remove(this.MessageSessionKey);
		}

	}
}
